﻿/*
 * This file is part of CommunitySettlers.
 *
 * CommunitySettlers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CommunitySettlers is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with CommunitySettlers.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Christoph Husse
 * 
 * Also checkout our homepage: http://communitysettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Markup;
using System.Windows.Threading;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;

namespace SettlersEngine
{
    internal class AnimationSetVisual : Grid
    {
        public List<Image> Images { get; private set; }
        public AnimationSet Set {get; private set;}
        public List<ISoundPlayer> Sounds { get; private set; }

        public AnimationSetVisual(AnimationSet inSet)
            : base()
        {
            if (inSet == null)
                throw new ArgumentNullException();

            Set = inSet;

            Sounds = new List<ISoundPlayer>();
            Images = new List<Image>(1);
            Width = Set.Width;
            Height = Set.Height;
            VerticalAlignment = System.Windows.VerticalAlignment.Top;
            HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

            foreach (var anim in Set.Animations)
            {
                Image image = new Image()
                {
                    Margin = new Thickness(anim.OffsetX, anim.OffsetY, 0, 0),
                    Width = anim.Width,
                    Height = anim.Height,
                    Stretch = Stretch.None,
                    VerticalAlignment = System.Windows.VerticalAlignment.Top,
                    HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                };

                Images.Add(image);
                Children.Add(image);
            }
        }
    }

    internal class AnimationSurface : Grid
    {
        private AnimationSet m_CurrentSet;
        public AnimationClass Class { get; private set; }
        public AnimationSetVisual AmbientOrEmptyVisual { get; private set; }
        public AnimationSetVisual CurrentVisual { get; private set; }

        public AnimationSet CurrentSet
        {
            get { return m_CurrentSet; }
            set
            {
                if (CurrentVisual != null)
                    Children.Remove(CurrentVisual);

                CurrentVisual = null;
                m_CurrentSet = value;

                if (CurrentSet != null)
                {
                    Children.Add(CurrentVisual = new AnimationSetVisual(CurrentSet));
                }
            }
        }

        ~AnimationSurface()
        {
            if (Class != null)
                Class.OnChanged -= DoChange;

            Class = null;
        }

        private void DoChange()
        {
            Width = Class.Width;
            Height = Class.Height;

            Children.Clear();

            // add ambient or empty set
            AmbientOrEmptyVisual = null;

            if((Class.EmptySet != null)/* && Class.UseEmptySet*/)
                AmbientOrEmptyVisual = new AnimationSetVisual(Class.EmptySet);
            else if ((Class.AmbientSet != null) /*&& Class.UseAmbientSet*/)
                AmbientOrEmptyVisual = new AnimationSetVisual(Class.AmbientSet);

            if (AmbientOrEmptyVisual != null)
                Children.Add(AmbientOrEmptyVisual);

            // overlay with current set
            CurrentSet = CurrentSet;

        }

        public AnimationSurface(AnimationClass inClass)
            : base()
        {
            if (inClass == null)
                throw new ArgumentNullException();

            Class = inClass;
            Class.OnChanged += DoChange;

            VerticalAlignment = System.Windows.VerticalAlignment.Top;
            HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            ClipToBounds = true;

            DoChange();
        }

    }
}
